package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;

import com.wing.productionPlan.model.entity.ProQualityTask;
import com.wing.productionPlan.model.request.ProQualityTaskAddForm;
import com.wing.productionPlan.model.request.ProQualityTaskUpdateForm;
import com.wing.productionPlan.model.response.ProQualityTaskVo;
import com.wing.productionPlan.service.ProQualityTaskService;

import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 25923
 */
@Slf4j
@Validated
@Api(value = "Admin-首件检测任务列表", tags = "#/product/qualityTask/taskDidInfo")
@RestController("productionQualityTaskController")
@RequestMapping("quality/admin/firstTask")
@RequiredArgsConstructor
public class ProductionQualityTaskController {

    private final ProQualityTaskService proQualityTaskService;;

    @ApiOperation(value = "查询首件检测任务列表", notes = "搜索-首件任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProQualityTaskVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            , String processCode
            , String workOrderCode
            , String code
            , Long sjPersion
            , Long jcPersion
            , String status
    ){
        Page<ProQualityTaskVo> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("processCode", processCode);
        map.put("workOrderCode", workOrderCode);
        map.put("code", code);
        map.put("sjPersion", sjPersion);
        map.put("cjPersion", jcPersion);
        map.put("status", status);
        Page<ProQualityTaskVo> list = proQualityTaskService.pageList4app(pg, map);
        list.getRecords().forEach(info->{
            if (info.getStatus() != null) {
                info.setStatusName(MaintenanceEnum.QualityStatus.getNameByCode(Integer.parseInt(info.getStatus())));
            }
        });
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加首件检测任务", notes = "添加-首件检测")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProQualityTaskAddForm.class) ProQualityTaskAddForm proQualityTaskAddForm) {
        if (proQualityTaskService.add(proQualityTaskAddForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改首件检测任务", notes = "修改-首件检测")
    @PostMapping("/update")
    public JsonResult update(@Validated(ProQualityTaskUpdateForm.class) ProQualityTaskUpdateForm proQualityTaskUpdateForm) {
        ProQualityTask proQualityTask = proQualityTaskService.getById(proQualityTaskUpdateForm.getId());
        if(proQualityTask == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProQualityTask.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(proQualityTaskUpdateForm, proQualityTask, copyOptions);
        proQualityTask.setUpdateTime(LocalDateTime.now());
        proQualityTask.setModifier(JwtUtils.getLoginUserId());
        proQualityTask.setModifierName(JwtUtils.getLoginUsername());
        if (proQualityTaskService.updateById(proQualityTask)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "关闭-检测任务", notes = "关闭-检测任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long id) {
        if (proQualityTaskService.closeTask(id)) {
            return JsonResult.success("操作成功！");
        }
        return JsonResult.fail("操作失败");
    }
}
